/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.doeacc.tms.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 *
 * @author slok
 */
@Entity(name = "ShippingInvoices")
public class ShippingInvoice implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Temporal(TemporalType.TIME)
    private Date invoiceDate = Calendar.getInstance().getTime();
    @ManyToOne
    private Customer consignee;
    @ManyToOne
    private Customer consignor;
    private String shippingFrom;
    private String shippingTo;
    @ManyToOne
    private User createdBy;
    private Double paidAmount = 0d;
    private Boolean toPay;
    private Double extraCharge = 0d;
    private Double discount = 0d;
    @OneToMany(mappedBy = "invoice", cascade = CascadeType.ALL)
    private List<ShippingInvoiceItem> items = new ArrayList<ShippingInvoiceItem>();

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Customer getConsignee() {
        return consignee;
    }

    public void setConsignee(Customer consignee) {
        this.consignee = consignee;
    }

    public Customer getConsignor() {
        return consignor;
    }

    public void setConsignor(Customer consignor) {
        this.consignor = consignor;
    }

    public User getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(User createdBy) {
        this.createdBy = createdBy;
    }

    public Double getDiscount() {
        return discount;
    }

    public void setDiscount(Double discount) {
        this.discount = discount;
    }

    public Double getExtraCharge() {
        return extraCharge;
    }

    public void setExtraCharge(Double extraCharge) {
        this.extraCharge = extraCharge;
    }

    public Date getInvoiceDate() {
        return invoiceDate;
    }

    public void setInvoiceDate(Date invoiceDate) {
        this.invoiceDate = invoiceDate;
    }

    public List<ShippingInvoiceItem> getItems() {
        return items;
    }

    public void setItems(List<ShippingInvoiceItem> items) {
        this.items = items;
    }

    public Double getPaidAmount() {
        return paidAmount;
    }

    public void setPaidAmount(Double paidAmount) {
        this.paidAmount = paidAmount;
    }

    public String getShippingFrom() {
        return shippingFrom;
    }

    public void setShippingFrom(String shippingFrom) {
        this.shippingFrom = shippingFrom;
    }

    public String getShippingTo() {
        return shippingTo;
    }

    public void setShippingTo(String shippingTo) {
        this.shippingTo = shippingTo;
    }

    public Boolean getToPay() {
        return toPay;
    }

    public void setToPay(Boolean toPay) {
        this.toPay = toPay;
    }

    public Double getTotalAmount() {
        Double total = 0d;
        if (this.items != null) {
            for (ShippingInvoiceItem item : this.items) {
                total += item.getChargedWeight() * item.getRate();
            }
        }
        return total;
    }

    @Override
    public int hashCode() {
        int hash = 180;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof ShippingInvoice)) {
            return false;
        }
        ShippingInvoice other = (ShippingInvoice) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    public String getType() {
        return "invoice";
    }
    
    @Override
    public String toString() {
        return "ShippingInvoice[#=" + id + "]";
    }
}
